package me.monster.smarttraffic.ui.road;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import me.monster.smarttraffic.base.BasePresenter;
import me.monster.smarttraffic.bo.RoadLight;
import me.monster.smarttraffic.net.RemoteService;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-14 10:19
 */
public class RoadPresenter extends BasePresenter<RoadContract.View> implements RoadContract.Presenter {
    public static final int ERROR_LIST_RULE = 100;
    public static final int ERROR_LIST_LIGHT = 200;
    public static final int ERROR_COMPARE = 300;
    private static final String TAG = "RoadPresenter";

    private List<RoadLight> cacheLightList = new ArrayList<>();

    @Override
    public void fetchLightRules() {
        iView.showLoading();
        Disposable listRuleDis = RemoteService.getInstance().listLightRule()
                .map(new Function<List<String>, List<Map<String, String>>>() {
                    @Override
                    public List<Map<String, String>> apply(List<String> strings) throws Exception {
                        List<Map<String, String>> maps = new ArrayList<>(strings.size());
                        for (String string : strings) {
                            Map<String, String> map = new HashMap<>(1);
                            map.put("name", string);
                            map.put("ruleSign", String.valueOf(strings.indexOf(string)));
                            maps.add(map);
                        }
                        return maps;
                    }
                })
                .subscribe(new Consumer<List<Map<String, String>>>() {
                    @Override
                    public void accept(List<Map<String, String>> maps) throws Exception {
                        iView.dismissLoading();
                        iView.bindSpinner(maps);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        iView.dismissLoading();
                        iView.showError(ERROR_LIST_RULE);
                        handleError(throwable);
                    }
                });
        addDispose(listRuleDis);
    }

    /**
     * 先点查询，后选择排序规则
     * 红 -> 升降
     * 绿 -> 升降
     * 黄 -> 升降
     *
     * @param compareRule
     */
    @Override
    public void compareLights(int compareRule) {
        switch (compareRule) {
            case 0:
                Collections.sort(cacheLightList, (roadLight, t1) -> Integer.compare(roadLight.getRed(), t1.getRed()));
                break;
            case 1:
                Collections.sort(cacheLightList, (roadLight, t1) -> Integer.compare(t1.getRed(), roadLight.getRed()));
                break;
            case 2:
                Collections.sort(cacheLightList, (roadLight, t1) -> Integer.compare(roadLight.getGreen(), t1.getGreen()));
                break;
            case 3:
                Collections.sort(cacheLightList, (roadLight, t1) -> Integer.compare(t1.getGreen(), roadLight.getGreen()));
                break;
            case 4:
                Collections.sort(cacheLightList, (roadLight, t1) -> Integer.compare(roadLight.getYellow(), t1.getYellow()));
                break;
            case 5:
                Collections.sort(cacheLightList, (roadLight, t1) -> Integer.compare(t1.getYellow(), roadLight.getYellow()));
                break;
            default:
        }
        iView.showLights(cacheLightList);
    }

    /**
     * 先选择排序规则，然后点击查询
     *
     * @param compareRule
     */
    @Override
    public void fetchLightAndCompare(int compareRule) {

    }

    /**
     * 先选择排序规则，然后点击查询
     */
    @Override
    public void fetchLight() {
        iView.showLoading();
        Disposable lightList = RemoteService.getInstance().listLight()
                .map(new Function<List<RoadLight>, List<RoadLight>>() {
                    @Override
                    public List<RoadLight> apply(List<RoadLight> lightList) throws Exception {
                        return generateRandomLightTime(lightList);
                    }
                }).subscribe(new Consumer<List<RoadLight>>() {
                    @Override
                    public void accept(List<RoadLight> lightList) throws Exception {
                        cacheLightList.clear();
                        cacheLightList.addAll(lightList);
                        iView.dismissLoading();
                        iView.showLights(lightList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        handleError(throwable);
                        iView.showError(ERROR_LIST_LIGHT);
                        iView.dismissLoading();
                    }
                });
        addDispose(lightList);
    }

    private List<RoadLight> generateRandomLightTime(List<RoadLight> lightList) {
        Random random = new Random();
        for (int i = 0; i < lightList.size(); i++) {
            RoadLight roadLight = lightList.get(i);
            roadLight.setRed(random.nextInt(60));
            roadLight.setYellow(random.nextInt(10));
            roadLight.setGreen(roadLight.getYellow() + 30);
        }
        return lightList;
    }
}
